#include "PlatformProperty.h"
#include "UnitUtil.h"
using namespace ecarx::vhal::core::property::UnitUtil;

//3.2.2 [SW_FC_005_004]单位设置
SYSTEM_READ_WRITE_PROPERTY(TIRE_PRESSURE_DISPLAY_UNITS, int32_t) {
    builder.area(VehicleArea::GLOBAL)
            .initValue([]() {
                VHALOG.d(
                    "withValueBySignals TIRE_PRESSURE_DISPLAY_UNITS : cc161[%d] "
                    "usgmod[%d]",
                    SIGNAL.getcarconfig161(), SIGNAL.getVehModMngtGlbSafe1UsgModSts());
                if (SIGNAL.getcarconfig161() == sdb::CarConfig161(-1)) {
                    VHALOG.d("TIRE_PRESSURE_DISPLAY_UNITS getcarconfig161 failed ,default: KILOPASCAL");
                    return toInt(VehicleUnit::KILOPASCAL);
                }
                if (SIGNAL.carConfigIs(
                        SignalRX::carconfig161, sdb::CarConfig161::_01, sdb::CarConfig161::_02,
                        sdb::CarConfig161::_03, sdb::CarConfig161::_04, sdb::CarConfig161::_09, 
                        sdb::CarConfig161::_0A, sdb::CarConfig161::_0B, sdb::CarConfig161::_0C, 
                        sdb::CarConfig161::_11, sdb::CarConfig161::_12, sdb::CarConfig161::_13,
                        sdb::CarConfig161::_14, sdb::CarConfig161::_19, sdb::CarConfig161::_1A,
                        sdb::CarConfig161::_1B, sdb::CarConfig161::_1C)) {
                    VHALOG.d("TIRE_PRESSURE_DISPLAY_UNITS : KILOPASCAL PA ");
                    return toInt(VehicleUnit::KILOPASCAL);
                } else {
                    VHALOG.d("TIRE_PRESSURE_DISPLAY_UNITS : PSI ");
                    return toInt(VehicleUnit::PSI);
                }
            }, PersistType::Profile)
           .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &last) {
                    if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                        return PropertyStatus::Active;
                    }
                    return PropertyStatus::NotActive;
                },
                SignalRX::VehModMngtGlbSafe1UsgModSts)
            .withValueBySignals([]([[maybe_unused]] const int32_t &last) {
                    VHALOG.d("withValueBySignals TIRE_PRESSURE_DISPLAY_UNITS : last[%d]", last);
                    return last;
                })
            .onSetProperty([this](const int32_t &v) {
                auto lastUnit = getValue(toInt(VehicleArea::GLOBAL));
                VHALOG.d("HalOnSetProperty TIRE_PRESSURE_DISPLAY_UNITS : [%d] lastUnit[%d]", v, lastUnit);

                if (v == toInt(VehicleUnit::KILOPASCAL)) {
                    SIGNAL.setTirePressureUnit(sdb::TriePUnit::KPa);
                } else if (v == toInt(VehicleUnit::PSI)) {
                    SIGNAL.setTirePressureUnit(sdb::TriePUnit::PSI);
                } else if (v == toInt(VehicleUnit::BAR)) {
                    SIGNAL.setTirePressureUnit(sdb::TriePUnit::Bar);
                } else {
                    VHALOG.d("HalOnSetProperty TIRE_PRESSURE_DISPLAY_UNITS  Parameter not supported ");
                    return;
                }

                setValue(toInt(VehicleArea::GLOBAL), v, true);
                if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                    VHALOG.d("HalOnSetProperty TIRE_PRESSURE_DISPLAY_UNITS : setCB_VFC_SetVehMisc");
                    SIGNAL.setCB_VFC_SetVehMisc(sdb::OnOff1::On);
                }
                IndcnUnit indcnUnit{};
                getCurrentIndcnUnit(indcnUnit, this);
                SIGNAL.setIndcnUnit(indcnUnit);

            });
}